package com.yimingkeji.security.web;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Describe:
 * @Auther: zhenlong.yang
 * @Date: 2019/1/3 13:55
 */
@Controller
public class IndexResource {

    @RequestMapping({"", "/", "/index"})
    @ResponseBody
    public String index(){
        return "hello 你好 <p>点击 <a th:href='@{/login}'>这里</a> 登录</p>";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/home")
    public String home(Model model){
        User principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        model.addAttribute("username", principal.getUsername());
        return "index";
    }

    @RequestMapping("/loginout")
    public String loginout(){
        SecurityContextHolder.clearContext();
        return "login";
    }
}
